﻿using System;
using System.Collections.Generic;
using DeepEarth.Map.Core.Tiling;
using System.Linq;

namespace DeepEarth.Map.TileSources
{
    public class QuadKeyTileSourceFactory : BaseTileSourceFactory
    {
		public readonly static MapMode QuadKey = new MapMode("quadKey", "quadKey", "QuadKey", "Quad Key", 0, false, true, true);

		private static readonly MapMode[] _availableModes = new[] { QuadKey };
		public override IList<MapMode> AvailableModes { get { return _availableModes; } }

		public readonly QuadKeyTileSource QuadKeyTileSource = new QuadKeyTileSource(QuadKey);

		public static bool IsValidMode(MapMode mode)
		{
			return _availableModes.Contains(mode);
		}

		public static void ValidateMode(MapMode mode)
		{
			if (!IsValidMode(mode))
				throw new ArgumentOutOfRangeException(String.Format("Map mode '{0}:{1}' is not valid for the QuadKey provider",
																	mode.ProviderKey,
																	mode.Identifier));
		}

		public override BaseTileSource CreateTileSource(MapMode mode)
		{
			mode = ValidateAndCoerceMapMode(mode);
			if (mode == QuadKey)
				return QuadKeyTileSource;

			throw new NotImplementedException("A tile provider for the mode '" + mode.Name + "' is not implemented.");
		}
	}
}
